#!/usr/bin/env python

import SocketServer
from ESL import *

class ESLRequestHandler(SocketServer.BaseRequestHandler ):
	def setup(self):
		print self.client_address, 'connected!'

		fd = self.request.fileno()
		print fd

		con = ESLconnection(fd)
		print "Connected: " 
		print  con.connected()
		if con.connected():

			con.filter("plain", "all")
			info = con.getInfo()

			uuid = info.getHeader("unique-id")
			con.filter("Unique-ID", uuid)
			con.events("plain", "DTMF_SEND DTMF RING_ASR PLAYBACK_STOP RECORD_START RECORD_STOP CHANNEL_PARK CHANNEL_ANSWER CHANNEL_EXECUTE_COMPLETE CHANNEL_HANGUP CHANNEL_BRIDGE CHANNEL_UNBRIDGE CUSTOM conference::maintenance");

			con.execute("set", "park_after_bridge=true", uuid)
			con.execute("set", "hangup_after_bridge=false", uuid)
			con.execute("set", "rtp_timeout_sec=60", uuid)
			con.execute("set", "playback_delimiter=&", uuid)
			con.execute("set", "playback_terminators=a", uuid)
			con.execute("set", "tts_engine=tts_commandline", uuid)
			con.execute("set", "tts_voice=ting", uuid)

			con.sendRecv("linger")
			apistr='uuid_pre_answer ' + uuid
			con.api(apistr)

			record_path = '/app/dongsn/recordings/'
			record_name = uuid + '.wav'
			api_seq = ('uuid_record', uuid, 'start', record_path + record_name)
			apistr =' '.join(api_seq)
			con.api(apistr)
			con.execute("playback", "/app/dongsn/src/woshizhendeaishangni.wav", uuid);

			con_client = ESLconnection("localhost","8690","ClueCon")
			if con_client.connected:
				e = con_client.api("create_uuid")
				if e:
					print e.getBody()
					uuid_client = e.getBody()
					uuid_client = uuid_client[:-2]
					
					con_client.events("plain", "DTMF_SEND DTMF RING_ASR PLAYBACK_STOP CHANNEL_ORIGINATE CHANNEL_PROGRESS_MEDIA CHANNEL_PROGRESS RECORD_START RECORD_STOP CHANNEL_PARK CHANNEL_ANSWER CHANNEL_EXECUTE_COMPLETE CHANNEL_HANGUP CHANNEL_BRIDGE CHANNEL_UNBRIDGE CUSTOM conference::maintenance");

					con_client.filter("plain", "all")
					con_client.filter("Unique-ID", uuid_client)
					dial_seq = ('originate {origination_uuid=', uuid_client, ',inherit_codec=true,codec_string=^^:PCMU}sofia/internal/fs20000004@192.168.21.21:8880 &park()')
					dialstr = ''.join(dial_seq)
					print dialstr
					con_client.bgapi(dialstr)

					while(con_client.connected):
						e_client = con_client.recvEvent()
						e_name = e_client.getHeader('event-name')

						if(e_name == 'CHANNEL_ORIGINATE'):
							print e_name
							con_client.execute("set", "continue_on_fail=true", uuid_client)
							con_client.execute("set", "park_after_bridge=true", uuid_client)
							con_client.execute("set", "playback_terminators=a", uuid_client)
							con_client.execute("set", "hangup_after_bridge=false", uuid_client)
							con_client.execute("set", "playback_delimiter=&", uuid_client)
							con_client.execute("set", "tts_engine=tts_commandline", uuid_client)
							con_client.execute("set", "tts_voice=ting", uuid_client)
							con_client.execute("set", "rtp_timeout_sec=60", uuid_client)

						#if(e_name == 'CHANNEL_PROGRESS_MEDIA' or e_name == 'CHANNEL_PROGRESS'):
						if(e_name == 'CHANNEL_PARK'):
							print "uuid_bridge",e_name
							apistr_client = 'uuid_bridge ' + uuid + ' ' + uuid_client
							con_client.api(apistr_client)

						if(e_name == 'CHANNEL_ANSWER'):
							con_client.execute("set", "rtp_timeout_sec=20", uuid_client)
							con.execute("set", "absolute_codec_string=:PCMU", uuid)
							con.execute("answer")
							con_client.api('echo Send_Clpms_Health_keep')

						if(e_name == 'CHANNEL_HANGUP'):
							con_client.disconnect()
							break;
												
			while(con.connected()):
				e = con.recvEvent()
				e_name = e.getHeader('event-name')
				print 'sdfdsf', e_name
				if(e_name == 'CHANNEL_HANGUP'):
					con.disconnect()
					print 'disconnect...'			
					break



		
#server host is a tuple ('host', port)
server = SocketServer.ThreadingTCPServer(('', 8899), ESLRequestHandler)
server.serve_forever()


